package com.xr.singleton.utils;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于转换输出对象
 */
public class JsonUtils {
    protected static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(JsonUtils.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtils() {
    }

    static {
        /**重写objectMapper 将null转换为"" **/
        objectMapper.getSerializerProvider().setNullValueSerializer(
                new JsonSerializer<Object>() {
                    @Override
                    public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
                        jsonGenerator.writeString("");
                    }
                }
        );
    }

    /**
     * 方法名称: obj2Str
     * 方法描述: 将对象转换为JSON
     * 返回类型: T
     *
     * @throws
     */
    @SuppressWarnings("unchecked")
    public static String obj2Str(Object object) {
        String jsonStr = null;
        try {
            jsonStr = objectMapper.writeValueAsString(object);
        } catch (JsonGenerationException e) {
            logger.error("JsonUtils Object2JSON JsonGenerationException", e);
        } catch (JsonMappingException e) {
            logger.error("JsonUtils Object2JSON JsonMappingException", e);
        } catch (IOException e) {
            logger.error("JsonUtils Object2JSON IOException", e);
        }
        return jsonStr;
    }

    /**
     * 方法名称: str2Obj
     * 方法描述: 将JSON转换为对象
     * 返回类型: T
     *
     * @throws
     */
    @SuppressWarnings("unchecked")
    public static <T extends Object> T str2Obj(String JSONStr, Class<T> clazz) {
        Object object = null;
        try {
            object = objectMapper.readValue(JSONStr, clazz);
        } catch (JsonGenerationException e) {
            logger.error("JsonUtils JSON2Object JsonGenerationException", e);
        } catch (JsonMappingException e) {
            logger.error("JsonUtils JSON2Object JsonMappingException", e);
        } catch (IOException e) {
            logger.error("JsonUtils JSON2Object IOException", e);
        }
        return object != null ? (T) object : null;
    }

    /**
     * 方法名称: str2List
     * 方法描述:  JSON 转换为List:
     * 返回类型: List<T>
     *
     * @throws
     */
    @SuppressWarnings("deprecation")
    public static <T> List<T> str2List(String jsonStr, Class<T> clazz) {
        List<T> result = null;
        try {
            JavaType t = objectMapper.getTypeFactory().constructParametricType(List.class, clazz);
            result = objectMapper.readValue(jsonStr, t);
        } catch (JsonGenerationException e) {
            logger.error("JsonUtils JSON2List JsonGenerationException", e);
        } catch (JsonMappingException e) {
            logger.error("JsonUtils JSON2List JsonMappingException", e);
        } catch (IOException e) {
            logger.error("JsonUtils JSON2List IOException", e);
        }
        return result == null ? new ArrayList<T>() : result;
    }


}
